package com.oocl.homework0712.multithreaddownload.thread;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.Random;

import com.oocl.homework0712.multithreaddownload.Start;

public class ThreadOut implements Runnable{
	private File file;
	private int id;
	private byte[] buf;
	
	public ThreadOut(File file, int id, byte[] buf) {
		super();
		this.file = file;
		this.id = id;
		this.buf = buf;
	}

	public void run() {
		// TODO Auto-generated method stub
		try {
			synchronized (file) {
				if(buf[4]!=0){
				OutputStream out  = new FileOutputStream(file);
				RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
				accessFile.seek(id*buf.length);
				accessFile.write(buf);
				Start.sleep(1000);
				String dString = new String(buf);
				System.out.println(dString);
				accessFile.close();
				out.close();
				int i = 0;
				while(i++<739){
					buf[i]=0;
				}
				//buf.notify();;
				}
				else {
					//buf.wait();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
